
package org.greenmango.service;

import com.mongodb.*;
import com.mongodb.util.JSON;
import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;

/*
 * Copyright (c) 2013 Sony John 
 * Contact me @ greenmangoquery@gmail.com
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
 *  this software and associated documentation files (the "Software"), to deal in
 *  the Software without restriction, including without limitation the rights to
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 *  the Software, and to permit persons to whom the Software is furnished to do so,
 *  subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in all
 *  copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
public class DbServiceImpl  implements DbService{
    private Logger logger = Logger.getLogger(DbServiceImpl.class.getName());
    @Autowired
private MongoClient mongoClient;

    @Override
    public Set<String> getCollections(String dbName) {
       DB db = mongoClient.getDB(dbName);
		return db.getCollectionNames();
    }

    @Override
    public List<String> getDbs() {
       if (mongoClient == null)
       {
           throw new RuntimeException("Mongo Client is not initialized");
       }
       return mongoClient.getDatabaseNames();
       
    }
     @Override
public StringBuilder getAllDocuments( String dbname,
			String collectionName) {
		
		
		DB db = mongoClient.getDB(dbname);
		DBCollection coll = db.getCollection(collectionName);
		DBCursor cursor = coll.find();
                System.out.println(cursor.toString());
		StringBuilder bsonData = new StringBuilder();
                String output;
                try {
			while (cursor.hasNext()) {
				output = cursor.next().toString();
                                System.out.println(output);
				bsonData.append(output);
				

			}
		} finally {
			cursor.close();
		}
		return bsonData;

	}
    @Override
    public Map getDbMap() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String insertDocument(String json, String dbname, String collectionName) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String updateDocument(String json, String dbname, String collectionName, String id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String deleteDocument(String json, String dbname, String collectionName, String id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void insertDb(String db) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void updatetDb(String db, String updateDb) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void deleteDb(String db) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void insertCollection(String db, String collection) {
     	DBObject option = new BasicDBObject();
	DB dbs = mongoClient.getDB(db);
	dbs.createCollection(collection, option);
    }

    @Override
    public void updateCollection(String db, String collection, String updatecollection) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void deleteCollection(String db, String collection) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String getDocument(String dbname, String collection, String id) {
        DB db = mongoClient.getDB(dbname);
        DBObject orgDbObject = new BasicDBObject("_id", new ObjectId(id));
        DBCollection coll = db.getCollection(collection);
		DBObject found = coll.findOne(orgDbObject);
                return found.toString();
    }

    public StringBuilder getDocumentsByQuery(String db, String collection, String query) {
      logger.info("query" + query);
		
		StringBuilder bsonData = new StringBuilder();
		
		DBObject object = (DBObject) JSON.parse(query);
		DB dbs = mongoClient.getDB(db);
		DBCollection coll = dbs.getCollection(collection);
		DBCursor cursor = coll.find(object);
		
		String output = null;
		try {
			while (cursor.hasNext()) {
				output = cursor.next().toString();
				bsonData.append(output );
				

			}
		} finally {
			cursor.close();
		}
		return bsonData;
    }

}
